package com.weixin.paleituo.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseRecycleAdapter;
import com.weixin.paleituo.bean.AppoinmentBean;
import com.weixin.paleituo.eventbus.DeleteVisitedEvent;
import com.weixin.paleituo.help.RecycleViewHolder;
import com.weixin.paleituo.util.StringUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Author yichao
 * Time  2018/4/7 17:51
 * Dest  预约
 */

public class AppoinmentRecycleAdapter extends BaseRecycleAdapter<AppoinmentBean> {

    public AppoinmentRecycleAdapter(Context context, List<AppoinmentBean> datas) {
        super(context, datas, R.layout.item_appoinment_layout);
    }

    @Override
    protected void setData(RecycleViewHolder holder, AppoinmentBean appoinmentBean, final int position) {

     TextView time= holder.getItemView(R.id.tv_time_item_appoinment_layout);
   TextView tvAddress=  holder.getItemView(R.id.tv_address_appoinment_layout);
  ImageView ivHouseIcon= holder.getItemView(R.id.iv_houseicon_item_appoinment_layout);
 TextView houseAddress=    holder.getItemView(R.id.tv_house_address_item_appoinment_layout);
  ImageView ivConcat=   holder.getItemView(R.id.ic_concat_item_appoinment_layout);
  ImageView ivDel=   holder.getItemView(R.id.iv_del_item_appoimment_layout);
     final    int state = appoinmentBean.getState();

        if (state==1){
            String apTime = appoinmentBean.getApTime();
            time.setText(StringUtils.isEmpty(apTime)?"时间：":"时间："+apTime);
            ivDel.setVisibility(View.GONE);
            ivConcat.setVisibility(View.VISIBLE);


        }else if (state==2){
            String visitTime = appoinmentBean.getVisitTime();
            time.setText(StringUtils.isEmpty(visitTime)?"时间：":"时间："+visitTime);
            ivDel.setVisibility(View.VISIBLE);
            ivConcat.setVisibility(View.VISIBLE);

        }else if (state==3){
            String cancelTime = appoinmentBean.getCancelTime();
            time.setText(StringUtils.isEmpty(cancelTime)?"时间：":"时间："+cancelTime);
            ivDel.setVisibility(View.VISIBLE);
            ivConcat.setVisibility(View.GONE);
        }
        tvAddress.setText("地址："+(StringUtils.isEmpty(appoinmentBean.getAddress())?"":appoinmentBean.getAddress()));
        UIUtils.loadImageView(mContext,(StringUtils.isEmpty(appoinmentBean.getSurfaceUrl())?"":appoinmentBean.getSurfaceUrl()),ivHouseIcon);
        houseAddress.setText(StringUtils.isEmpty(appoinmentBean.getFullname())?"":appoinmentBean.getFullname());
        final String phone = appoinmentBean.getPhone();
        ivConcat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    Intent dialIntent =  new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phone));
                    mContext.startActivity(dialIntent);
                }catch (Exception e){
                    ToastUtils.makeText("还没有联系人号码");
                }

            }
        });
        ivDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //删除
                DeleteVisitedEvent deleteVisitedEvent=new DeleteVisitedEvent();
                deleteVisitedEvent.setPosition(position);
                deleteVisitedEvent.setState(state);
                EventBus.getDefault().post(deleteVisitedEvent);
            }
        });
    }
}
